<?php
 /*****************************************************************************
 * pdfInvoice.php                                                             *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * pdfInvoice.php is used to format the output of an invoice record before    *
 * converting it into an Acrobat (.pdf) file for download. pdfInvoice.php     *
 * should be called by a button "Download" in viewInvoices.php and then the   *
 * output passed to include/createPDF.php.                                    *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      pdfInvoice.php                                                  *
 * @version   1.0.0                                                           *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/
 
 
  // Require config.inc.php for configuration variables
	require_once "config.inc.php";

  // Connect to MySQL
	mysql_connect($path, $username, $password);
	// Select the database, if error die with error message
	@mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

  // Get invoiceNum from URL
	$invoiceNum = $_GET['invoiceNum'];

  // If invoiceNum = NULL...
	if($invoiceNum == "") {
    // Die with error message
		die('Cannot retrieve the invoice without the Invoice Number. Please enter the Invoice Number and <a href="invoices.php">try again</a>.');
  // If invoiceNum != NULL...
	} else {
    // Define MySQL query to select the invoice with the given invoice number
		$query = "SELECT * FROM tblInvoices WHERE invoiceNum='" . $invoiceNum . "' LIMIT 1";
		
		// Execute the MySQL query, if error die with error message
		$result = mysql_query($query) or die(mysql_error());

    // Determine the number of rows returned
		$num = mysql_numrows($result);

    // If num = 0 (no rows returned)
		if($num == 0) {
                 // Die with error message
                 die('Record not found. Please check the Invoice Number and <a href="invoices.php">try again</a>.');
    // If num != 0 (rows were returned)
		} else {
                 // Define data variables for the data from tblInvoices
                 $key = mysql_result($result,0,"key");
                 $date = mysql_result($result,0,"date");
                 $invoiceNum = mysql_result($result,0,"invoiceNum");
                 $customerID = mysql_result($result,0,"customerID");
			
                 $item1Qty = mysql_result($result,0,'item1Qty');
                 $item2Qty = mysql_result($result,0,'item2Qty');
                 $item3Qty = mysql_result($result,0,'item3Qty');
                 $item4Qty = mysql_result($result,0,'item4Qty');
                 $item5Qty = mysql_result($result,0,'item5Qty');
                 $item6Qty = mysql_result($result,0,'item6Qty');
                 $item7Qty = mysql_result($result,0,'item7Qty');
                 $item8Qty = mysql_result($result,0,'item8Qty');
                 $item9Qty = mysql_result($result,0,'item9Qty');
                 $item10Qty = mysql_result($result,0,'item10Qty');

                 $item1Name = mysql_result($result,0,'item1Name');
                 $item2Name = mysql_result($result,0,'item2Name');
                 $item3Name = mysql_result($result,0,'item3Name');
                 $item4Name = mysql_result($result,0,'item4Name');
                 $item5Name = mysql_result($result,0,'item5Name');
                 $item6Name = mysql_result($result,0,'item6Name');
                 $item7Name = mysql_result($result,0,'item7Name');
                 $item8Name = mysql_result($result,0,'item8Name');
                 $item9Name = mysql_result($result,0,'item9Name');
                 $item10Name = mysql_result($result,0,'item10Name');

                 $item1Rate = mysql_result($result,0,'item1Rate');
                 $item2Rate = mysql_result($result,0,'item2Rate');
                 $item3Rate = mysql_result($result,0,'item3Rate');
                 $item4Rate = mysql_result($result,0,'item4Rate');
                 $item5Rate = mysql_result($result,0,'item5Rate');
                 $item6Rate = mysql_result($result,0,'item6Rate');
                 $item7Rate = mysql_result($result,0,'item7Rate');
                 $item8Rate = mysql_result($result,0,'item8Rate');
                 $item9Rate = mysql_result($result,0,'item9Rate');
                 $item10Rate = mysql_result($result,0,'item10Rate');

                 $item1Total = mysql_result($result,0,'item1Total');
                 $item2Total = mysql_result($result,0,'item2Total');
                 $item3Total = mysql_result($result,0,'item3Total');
                 $item4Total = mysql_result($result,0,'item4Total');
                 $item5Total = mysql_result($result,0,'item5Total');
                 $item6Total = mysql_result($result,0,'item6Total');
                 $item7Total = mysql_result($result,0,'item7Total');
                 $item8Total = mysql_result($result,0,'item8Total');
                 $item9Total = mysql_result($result,0,'item9Total');
                 $item10Total = mysql_result($result,0,'item10Total');

                 $discount = mysql_result($result,0,'discount');
                 $tax = mysql_result($result,0,'tax');
                 $subTotal = mysql_result($result,0,'subTotal');
                 $total = mysql_result($result,0,'total');
                 $notes = mysql_result($result,0,'notes');
         
                 // Close the connection
                 mysql_close();

                 // Connect to MySQL
                 mysql_connect($path, $username, $password);
                 // Select the database, if error die with error message
                 @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());
			
                 // Define query to select the contact information for the given customerID
                 $query2 = "SELECT * FROM tblContacts WHERE customerID='" . $customerID . "' LIMIT 1";
                 
                 // Execut MySQL query, if error die with error message
                 $result2 = mysql_query($query2) or die(mysql_error());

                 // Determine number of rows returned
                 $num2 = mysql_numrows($result2);
			
                 // Define data variables for data from tblContacts
                 $company = mysql_result($result2,0,"company");
                 $firstName = mysql_result($result2,0,"firstName");
                 $lastName = mysql_result($result2,0,"lastName");
                 $address1 = mysql_result($result2,0,"address1");
                 $address2 = mysql_result($result2,0,"address2");
                 $city = mysql_result($result2,0,"city");
                 $state = mysql_result($result2,0,"state");
                 $zipCode = mysql_result($result2,0,"zipCode");
                 $phone1 = mysql_result($result2,0,"phone1");
                 $phone2 = mysql_result($result2,0,"phone2");
                 $fax = mysql_result($result2,0,"fax");
                 $email = mysql_result($result2,0,"email");
                 $webSite = mysql_result($result2,0,"webSite");
			
                 // Close the connection
                 mysql_close();
		}  // End if($num == 0)
	}  // End if($invoiceNum == ""

// Output the invoice data
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";

echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
	echo '<head>' . "\n";
		echo '<meta name="ROBOTS" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<meta name="GOOGLEBOT" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<title>Bugs Bee Gone - Billing - Invoice Number: ' . $invoiceNum . ' (' . $date . ')</title>' . "\n";
		echo '<meta name="description" content="Bugs Bee Gone - Billing - View Invoice" />' . "\n";
		echo '<link rel="shortcut icon" href="" />' . "\n";
		echo '<link href="invoice.css" type="text/css" rel="stylesheet" />' . "\n";

		echo '<script type="text/javascript" src="func.inc.js"></script>' . "\n";

		echo '<script type="text/javascript" src="print_page.js"></script>' . "\n";
		echo '<script type="text/javascript">' . "\n";
		echo 'function printPage() {' . "\n";
		echo 'createPage(document.getElementById("invoice").innerHTML, "http://www.bugsbeegone.com", "Bugs Bee Gone - Billing - ' . $invoiceNum . '", "");' . "\n";
		echo '}' . "\n";

		echo 'function createPDF() {' . "\n";
		echo 'newPDF(document.URL, "invoice_' . $invoiceNum . '_' . $date . '");' . "\n";
		echo '}' . "\n";
		echo '</script>' . "\n";
	echo '</head>' . "\n";
?>

	<body>

	<div id="invoice">
		<table width="700" border="0" align="center">
		<tr>
			<td valign="bottom" width="250" style="word-wrap: break-word;">
				<img  src="bbg_logo.gif" width="192" height="153" alt="Bugs Bee Gone logo" />
			</td>

			<td valign="bottom" width="550" style="word-wrap:break-word; text-align:left;">
				Bugs Bee Gone<br />
				PO Box 923<br />
				Mexico, Missouri 65265<br />
				573-721-3245<br />
				sales@bugsbeegone.com<br />
				http://www.bugsbeegone.com
			</td>
		</tr>

		<tr>
			<td valign="bottom" width="100%" colspan="2" style="font-weight:bold; font-size:12pt; text-align:right;">
				Invoice #: <?php echo $invoiceNum; ?>
			</td>
		</tr>
		
		<tr>
			<td valign="bottom" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:right;">
				<?php echo $date; ?><br />
				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:bold; font-size:10pt; text-align:left;">
				Bill to:
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:left;">
				<?php
				// If company != NULL...
        if($company != "") {
          // Output company name then customer's name
					echo '<b>' . $company . '</b><br />' . "\n";
					echo $firstName . ' ' . $lastName . '<br />' . "\n";
        // If comany = NULL...
				} else {
          // Output customer's name
					echo '<b>' . $firstName . ' ' . $lastName . '</b><br />' . "\n";
				}
				
				echo $address1 . '<br />' . "\n";
				// If address2 != NULL...
				if($address2 != "") {
          // Output address2
					echo $address2 . '<br />' . "\n";
				}
				
				echo $city . ', ' . $state . ' ' . $zipCode . '<br />' . "\n";
				echo 'Phone: ' . $phone1 . '<br />' . "\n";
				// If phone2 != NULL...
				if($phone2 != "") {
          // Output phone2
					echo 'Phone: ' . $phone2 . '<br />' . "\n";
				}
				// If fax != NULL...
				if($fax != "") {
          // Output fax
					echo 'Fax: ' . $fax . '<br />' . "\n";
				}
				// If email != NULL...
				if($email != "") {
          // Output email
					echo $email . '<br />' . "\n";
				}
				?>
				
				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:left;">
				<b>Terms:</b> Balance due within 30 days of invoice date. Overdue accounts will be subject to a 
				service charge of 10% per month (30 days).

				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="text-align:left;">
				<table width="100%" border="0">
					<tr>
						<th valign="top" width="100" class="invoice">Quantity</th>
						<th valign="top" width="400" class="invoice">Item</th>
						<th valign="top" width="100" class="invoice" style="text-align:right;">Rate</th>
						<th valign="top" width="100" class="invoice" style="text-align:right;">Total</th>
					</tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item1Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item1Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item1Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item1Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item2Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item2Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item2Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item2Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item3Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item3Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item3Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item3Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item4Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item4Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item4Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item4Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item5Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item5Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item5Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item5Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item6Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item6Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item6Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item6Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item7Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item7Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item7Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item7Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item8Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item8Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item8Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item8Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item9Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item9Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item9Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item9Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<tr>
						<td valign="top" width="100">
							<?php echo $item10Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item10Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item10Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item10Total; ?>
						</td>
					</tr>

					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Subtotal:
						</td>
						<td valign="top" style="text-align:right;">
							<?php echo $subTotal; ?>
						</td>
					</tr>

					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Discounts:
						</td>
						<td valign="top" style="text-align:right;">
							- <?php echo $discount; ?>
						</td>
					</tr>

					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Tax:
						</td>
						<td valign="top" style="text-align:right;">
							+ <?php echo $tax; ?>
						</td>
					</tr>

					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

					<tr>
						<td valign="top" colspan="3" style="text-align:right; font-weight:bold;">
							Balance Due:
						</td>
						<td valign="top" style="text-align:right; font-weight:bold;">
							$ <?php echo $total; ?>
						</td>
					</tr>

					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

					<?php
            // If notes != NULL...
						if($notes != "") {
              // Output notes
							echo '<tr>' . "\n";
								echo '<td valign="top" width="100%" colspan="4" style="text-align:left; font-weight:normal; font-size:10pt;">' . "\n";
									echo '<b><u>Notes:</u></b><br />' . "\n";
									echo $notes . "\n";
								echo '</td>' . "\n";
							echo '</tr>' . "\n";

							echo '<tr>' . "\n";
								echo '<td valign="middle" width="100%" colspan="4"><hr /></td>' . "\n";
							echo '</tr>' . "\n";
						}
					?>

				</table>
			</td>
		</tr>
		</table>
	</div>

	</body>
</html>